package cc.zzone.domain;

import cc.zzone.util.SpringUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.annotations.*;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * 弹幕配置对象
 *
 * @author jinzhengze
 * @since 2023年10月15日
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatConf implements Serializable {

    @Serial
    private static final long serialVersionUID = 22L;

    private Integer id;

    private String name;

    private String platform;

    private String room;

    private String display;

    private String shield;

    private String style;

    public String getKey() {
        return this.platform + ":" + this.room;
    }

    public static ChatConfMapper mapper() {
        return SpringUtil.getBean(ChatConfMapper.class);
    }

    @Mapper
    public interface ChatConfMapper {

        @Select("select * from `chat_conf` where `id` = #{id}")
        @Results(id = "result", value = {
                @Result(column = "id", property = "id", id = true),
                @Result(column = "name", property = "name"),
                @Result(column = "platform", property = "platform"),
                @Result(column = "room", property = "room"),
                @Result(column = "display", property = "display"),
                @Result(column = "shield", property = "shield"),
                @Result(column = "style", property = "style")})
        ChatConf getData(@Param("id") Integer id);

        @Select({"<script>",
                "select * from `chat_conf`",
                "<where>",
                "<if test=\"id != null\">and `id` = #{id}</if>",
                "<if test=\"name != null and name != ''\">and `name` = #{name}</if>",
                "<if test=\"platform != null and platform != ''\">and `platform` = #{platform}</if>",
                "<if test=\"room != null and room != ''\">and `room` = #{room}</if>",
                "</where>",
                "</script>"})
        @ResultMap("result")
        List<ChatConf> listData(ChatConf data);

        @Select({"<script>",
                "select * from `chat_conf`",
                "<where>",
                "<if test=\"id != null\">and `id` like concat(#{id}, '%')</if>",
                "<if test=\"name != null and name != ''\">and `name` like concat(#{name}, '%')</if>",
                "<if test=\"platform != null and platform != ''\">and `platform` like concat(#{platform}, '%')</if>",
                "<if test=\"room != null and room != ''\">and `room` like concat(#{room}, '%')</if>",
                "</where>",
                "</script>"})
        @ResultMap("result")
        List<ChatConf> likeData(ChatConf data);

        @Insert({"<script>",
                "insert into `chat_conf`",
                "(",
                "<if test=\"name != null and name != ''\">`name`,</if>",
                "<if test=\"room != null and room != ''\">`room`,</if>",
                "<if test=\"display != null and display != ''\">`display`,</if>",
                "<if test=\"shield != null and shield != ''\">`shield`,</if>",
                "<if test=\"style != null and style != ''\">`style`,</if>",
                "`platform`) values (",
                "<if test=\"name != null and name != ''\">#{name},</if>",
                "<if test=\"room != null and room != ''\">#{room},</if>",
                "<if test=\"display != null and display != ''\">#{display},</if>",
                "<if test=\"shield != null and shield != ''\">#{shield},</if>",
                "<if test=\"style != null\">#{style},</if>",
                "#{platform})",
                "</script>"})
        Integer insertData(ChatConf data);

        @Update({"<script>",
                "update `chat_conf`",
                "<set>",
                "<if test=\"name != null and name != ''\">`name` = #{name},</if>",
                "<if test=\"platform != null and platform != ''\">`platform` = #{platform},</if>",
                "<if test=\"room != null and room != ''\">`room` = #{room},</if>",
                "<if test=\"display != null and display != ''\">`display` = #{display},</if>",
                "<if test=\"shield != null and shield != ''\">`shield` = #{shield},</if>",
                "<if test=\"style != null\">`style` = #{style},</if>",
                "</set>",
                "where `id` = #{id}",
                "</script>"})
        Integer updateData(ChatConf data);

        @Delete({"<script>",
                "delete from `chat_conf` where id in",
                "<foreach item='id' collection='array' open='(' separator=',' close=')'>",
                "#{id}",
                "</foreach>",
                "</script>"})
        Integer deleteData(Integer... ids);
    }
}
